package com.acme.bankapp.util;

import java.util.regex.Pattern;
import java.util.regex.Matcher;

import com.acme.bankapp.domain.bank.AccountType;
import com.acme.bankapp.domain.bank.Gender;
import com.acme.bankapp.domain.client.ClientInfoProvider;

/**
 * This class is used for parsing feed file line
 * 
 * @author Anton Kotov
 * 
 */
public class FeedFileLineParser implements ClientInfoProvider {

    public FeedFileLineParser(String line) throws ParseException {

	Matcher matcher = pattern.matcher(line);

	if (!matcher.matches()) {
	    throw new ParseException();
	}

	accountId = Integer.parseInt(matcher.group(1));
	accountType = matcher.group(2).equals("c") ? AccountType.CHECKING
		: AccountType.SAVING;
	balance = Double.parseDouble(matcher.group(3));
	overdraft = Double.parseDouble(matcher.group(5));
	clientName = matcher.group(7);
	clientGender = matcher.group(8).equals("m") ? Gender.MALE
		: Gender.FEMALE;
    }

    public int getAccountId() {
	return accountId;
    }

    public AccountType getAccountType() {
	return accountType;
    }

    public double getBalance() {
	return balance;
    }

    public double getOverdraft() {
	return overdraft;
    }

    public String getClientName() {
	return clientName;
    }

    public Gender getClientGender() {
	return clientGender;
    }

    private int accountId;
    private AccountType accountType;
    private double balance;
    private double overdraft;
    private String clientName;
    private Gender clientGender;

    private static String doubleRegExp = "[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?";
    private static String regExp = "^accountid=(-?[0-9]+);accounttype=([cs]);balance=("
	    + doubleRegExp
	    + ");overdraft=("
	    + doubleRegExp
	    + ");name=(.+);gender=([mf]);$";
    static Pattern pattern = Pattern.compile(regExp);
}
